package com.donger.business.modules.upms.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donger.auth.utils.SecurityUtil;
import com.donger.business.enums.LoginTypeEnum;
import com.donger.business.modules.upms.entity.SysSocialDetails;
import com.donger.business.modules.upms.entity.SysUser;
import com.donger.business.modules.upms.handler.LoginHandler;
import com.donger.business.modules.upms.mapper.SysSocialDetailsMapper;
import com.donger.business.modules.upms.mapper.SysUserMapper;
import com.donger.business.modules.upms.service.ISysSocialDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@AllArgsConstructor
public class SysSocialDetailsServiceImpl extends ServiceImpl<SysSocialDetailsMapper,SysSocialDetails> implements ISysSocialDetailsService {
    private final Map<String, LoginHandler> loginHandlerMap;

    private final SysUserMapper sysUserMapper;

    @Override
    public SysUser socialUserInfo(String inStr) {
        String[] inStrs = inStr.split(StringPool.AT);
        String type = inStrs[0];
        String loginStr = inStrs[1];
        return loginHandlerMap.get(type).handle(loginStr);
    }

    /**
     * 用户绑定
     * @param type
     * @param code
     * @return
     */
    @Override
    public Boolean bindSocial(String type, String code) {
        String identify = loginHandlerMap.get(type).identify(code);
        SysUser sysUser = sysUserMapper.selectById(SecurityUtil.getUser().getUserId());
        if (LoginTypeEnum.GITEE.getType().equals(type)) {
            sysUser.setGiteeLogin(identify);
        } else if (LoginTypeEnum.WECHAT.getType().equals(type)) {
            sysUser.setWxOpenid(identify);
        }else if(LoginTypeEnum.DING.getType().equals(type)){
            sysUser.setDingId(identify);
        }
        sysUserMapper.updateById(sysUser);
        return Boolean.TRUE;
    }
}
